package cn.stylefeng.guns.modular.base.item.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.base.item.entity.Item;
import cn.stylefeng.guns.modular.base.item.enums.ItemExceptionEnum;
import cn.stylefeng.guns.modular.base.item.mapper.ItemMapper;
import cn.stylefeng.guns.modular.base.item.pojo.request.ItemRequest;
import cn.stylefeng.guns.modular.base.item.service.ItemService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 基础数据-存货档案业务实现层
 *
 * @author caoxf
 * @since 2023/11/20 22:56
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

	@Override
    public void add(ItemRequest itemRequest) {
        Item item = new Item();
        BeanUtil.copyProperties(itemRequest, item);
        this.save(item);
    }

    @Override
    public void del(ItemRequest itemRequest) {
        Item item = this.queryItem(itemRequest);
        this.removeById(item.getCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(ItemRequest itemRequest) {
        this.removeByIds(itemRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(ItemRequest itemRequest) {
        Item item = this.queryItem(itemRequest);
        BeanUtil.copyProperties(itemRequest, item);
        this.updateById(item);
    }

    @Override
    public Item detail(ItemRequest itemRequest) {
        return this.queryItem(itemRequest);
    }

    @Override
    public PageResult<Item> findPage(ItemRequest itemRequest) {
        LambdaQueryWrapper<Item> wrapper = createWrapper(itemRequest);
        Page<Item> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<Item> findList(ItemRequest itemRequest) {
        LambdaQueryWrapper<Item> wrapper = this.createWrapper(itemRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author caoxf
     * @since 2023/11/20 22:56
     */
    private Item queryItem(ItemRequest itemRequest) {
        Item item = this.getById(itemRequest.getCode());
        if (ObjectUtil.isEmpty(item)) {
            throw new ServiceException(ItemExceptionEnum.ITEM_NOT_EXISTED);
        }
        return item;
    }

    /**
     * 创建查询wrapper
     *
     * @author caoxf
     * @since 2023/11/20 22:56
     */
    private LambdaQueryWrapper<Item> createWrapper(ItemRequest itemRequest) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();

        String code = itemRequest.getCode();
        String itemcode = itemRequest.getItemcode();
        String itemname = itemRequest.getItemname();
        String specs = itemRequest.getSpecs();
        String classcode = itemRequest.getClasscode();
        String unitgroupcode = itemRequest.getUnitgroupcode();
        String puunitcode = itemRequest.getPuunitcode();
        String saunitcode = itemRequest.getSaunitcode();
        String stunitcode = itemRequest.getStunitcode();
        String barcode = itemRequest.getBarcode();
        String defwarehouse = itemRequest.getDefwarehouse();
        String vendor = itemRequest.getVendor();
        String itemformat = itemRequest.getItemformat();
        String schedulingtype = itemRequest.getSchedulingtype();
        String keylevel = itemRequest.getKeylevel();
        String model = itemRequest.getModel();
        String design = itemRequest.getDesign();
        String mlevel = itemRequest.getMlevel();

        queryWrapper.like(ObjectUtil.isNotEmpty(code), Item::getCode, code);
        queryWrapper.like(ObjectUtil.isNotEmpty(itemcode), Item::getItemcode, itemcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(itemname), Item::getItemname, itemname);
        queryWrapper.like(ObjectUtil.isNotEmpty(specs), Item::getSpecs, specs);
        queryWrapper.like(ObjectUtil.isNotEmpty(classcode), Item::getClasscode, classcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(unitgroupcode), Item::getUnitgroupcode, unitgroupcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(puunitcode), Item::getPuunitcode, puunitcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(saunitcode), Item::getSaunitcode, saunitcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(stunitcode), Item::getStunitcode, stunitcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(barcode), Item::getBarcode, barcode);
        queryWrapper.like(ObjectUtil.isNotEmpty(defwarehouse), Item::getDefwarehouse, defwarehouse);
        queryWrapper.like(ObjectUtil.isNotEmpty(vendor), Item::getVendor, vendor);
        queryWrapper.like(ObjectUtil.isNotEmpty(itemformat), Item::getItemformat, itemformat);
        queryWrapper.eq(ObjectUtil.isNotEmpty(schedulingtype), Item::getSchedulingtype, schedulingtype);
        queryWrapper.eq(ObjectUtil.isNotEmpty(keylevel), Item::getKeylevel, keylevel);
        queryWrapper.like(ObjectUtil.isNotEmpty(model), Item::getModel, model);
        queryWrapper.like(ObjectUtil.isNotEmpty(design), Item::getDesign, design);
        queryWrapper.eq(ObjectUtil.isNotEmpty(mlevel), Item::getMlevel, mlevel);

        return queryWrapper;
    }

}
